#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	int f = 0;
	for (int i = 2;i <= n;i++)
	{
		f = (f + m) % i;
	}
	cout << f << endl;
	return 0;
}